package com.dt76.smallloanbackendservice.service.afterloan.impl;


import com.dt76.smallloanbackendservice.mapper.afterloan.InspectionMapper;
import com.dt76.smallloanbackendservice.pojo.afterloan.InspectionCommitInfo;
import com.dt76.smallloanbackendservice.pojo.afterloan.InspectionInfo;
import com.dt76.smallloanbackendservice.pojo.afterloan.InspectionSimpleLoanMessage;
import com.dt76.smallloanbackendservice.pojo.afterloan.RepaymentQualification;
import com.dt76.smallloanbackendservice.service.afterloan.InspectionService;
import com.dt76.smallloanbackendservice.utils.IdWorker;
import com.dt76.smallloanbackendservice.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("inspectionService")
public class InspectionServiceImpl implements InspectionService {

    private final IdWorker idWorker;
    private final InspectionMapper inspectionMapper;

    public InspectionServiceImpl(InspectionMapper inspectionMapper, IdWorker idWorker) {
        this.inspectionMapper = inspectionMapper;
        this.idWorker = idWorker;
    }

    @Override
    public PageResult qryAll(RepaymentQualification q, Integer cuPage, Integer size) {
        Page page = PageHelper.startPage(cuPage, size);
        List<InspectionInfo> list = inspectionMapper.getPageList(q);
        PageInfo info = new PageInfo<>(page.getResult());
        return new PageResult(info.getTotal(), list);
    }

    @Override
    public Integer addCommitInfo(InspectionCommitInfo commitInfo) {
        commitInfo.setReportId(Long.toString(idWorker.nextId()));
        return inspectionMapper.addCheckInfo(commitInfo);
    }

    @Override
    public List<InspectionSimpleLoanMessage> getAllSLM() {
        List<InspectionSimpleLoanMessage> list = inspectionMapper.qryAllSimpleLoanMessage();
        for (InspectionSimpleLoanMessage message : list) {
            message.setValue(message.getLoanId() + " " + message.getUserName());
        }
        return list;
    }

    @Override
    public List<InspectionSimpleLoanMessage> getTheSLM(InspectionCommitInfo info) {
        return inspectionMapper.qryTheSimpleLoanMessage(info);
    }
}
